
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using Apps.Models;
using Apps.Common;
using Microsoft.Practices.Unity;
using System.Transactions;
using Apps.BLL.Core;
using Apps.Locale;
using LinqToExcel;
using System.IO;
using System.Text;
using System.Linq.Expressions;
using Apps.IDAL.PUB;
using Apps.Models.PUB;
using Apps.IBLL.PUB;
namespace Apps.BLL.PUB
{
	public partial class PUB_SCHEMEBLL: Virtual_PUB_SCHEMEBLL,IPUB_SCHEMEBLL
	{
        

	}
	public class Virtual_PUB_SCHEMEBLL
	{
        [Dependency]
        public IPUB_SCHEMERepository m_Rep { get; set; }

		public virtual List<PUB_SCHEMEModel> GetList(ref GridPager pager, string queryStr)
        {

            IQueryable<PUB_SCHEME> queryData = null;
            if (!string.IsNullOrWhiteSpace(queryStr))
            {
                queryData = m_Rep.GetList(
								a=>  a.COMPILE_USER.Contains(queryStr)
								|| a.SCHEME_TYPE.Contains(queryStr)
								);
            }
            else
            {
                queryData = m_Rep.GetList();
            }
            pager.totalRows = queryData.Count();
            //排序
            queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
            return CreateModelList(ref queryData);
        }
		 
		public virtual List<PUB_SCHEMEModel> GetPagerList(ref GridPager pager, Expression<Func<PUB_SCHEME, bool>> whereLambda )
        {

            IQueryable<PUB_SCHEME> queryData = m_Rep.GetList(whereLambda); 
             
            pager.totalRows = queryData.Count();
            //排序
            queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
            return CreateModelList(ref queryData);
        }

		public virtual List<PUB_SCHEMEModel> GetList(Expression<Func<PUB_SCHEME, bool>> whereLambda )
        {

            IQueryable<PUB_SCHEME> queryData = m_Rep.GetList(whereLambda); 
       
            return CreateModelList(ref queryData);
        }

		public virtual List<PUB_SCHEMEModel> GetListByParentId(ref GridPager pager, string queryStr,object parentId)
        {
			return new List<PUB_SCHEMEModel>();
		}

        public virtual List<PUB_SCHEMEModel> CreateModelList(ref IQueryable<PUB_SCHEME> queryData)
        {

            List<PUB_SCHEMEModel> modelList = (from r in queryData
                                              select new PUB_SCHEMEModel
                                              {
													SCHEME_ID = r.SCHEME_ID,
													SCHEME_TYPE = r.SCHEME_TYPE,
													SCHEME_MARK = r.SCHEME_MARK,
													EFFECTIVE_DATE = r.EFFECTIVE_DATE,
													LAYOUT_TYPE = r.LAYOUT_TYPE,
													CAR_COUNT = r.CAR_COUNT,
													COAL_TOTAL = r.COAL_TOTAL,
													POINT_COUNT = r.POINT_COUNT,
													POINT_SUM = r.POINT_SUM,
													COMPILE_USER = r.COMPILE_USER,
													COMPILE_DATE = r.COMPILE_DATE,
													STATUS = r.STATUS,
													
                                              }).ToList();

            return modelList;
        }

        public virtual bool Create(ref ValidationErrors errors, PUB_SCHEMEModel model)
        {
            try
            {
                PUB_SCHEME entity = m_Rep.GetById(model.CAR_ID);
                if (entity != null)
                {
                    errors.Add(Resource.PrimaryRepeat);
                    return false;
                }
                entity = new PUB_SCHEME();
               				entity.CAR_ID = model.CAR_ID;
				entity.RFID =string.IsNullOrEmpty(model.RFID)?"0": model.RFID;
				entity.LPR = model.LPR;
				entity.DRIVER_IDCARD = string.IsNullOrEmpty(model.DRIVER_IDCARD) ? "000000000000000000" : model.DRIVER_IDCARD;
                entity.DRIVER_PHONE = string.IsNullOrEmpty(model.DRIVER_PHONE) ? "00000000000" : model.DRIVER_PHONE;
                entity.SPECIFIED_WEIGHT = model.SPECIFIED_WEIGHT == null ? 0 : model.SPECIFIED_WEIGHT;
                entity.TARE =model.TARE==null ? 0 : model.TARE;
                entity.TOTLE_LENGTH = model.TOTLE_LENGTH== null ? 0 : model.TOTLE_LENGTH;
                entity.TOTLE_WIDTH = model.TOTLE_WIDTH==null ? 0 : model.TOTLE_WIDTH;
                entity.LENGTH = model.LENGTH;
				entity.WIDTH = model.WIDTH;
				entity.HEIGHT = model.HEIGHT;
				entity.POSTIONS = model.POSTIONS;
				entity.REGISTER_USER_ID = model.REGISTER_USER_ID;
				entity.REGISTER_TIME = model.REGISTER_TIME;
				entity.UPDATE_USER_ID = model.UPDATE_USER_ID;
				entity.UPDATE_TIME = model.UPDATE_TIME;
				entity.STATUS = model.STATUS;
				entity.UNREGISTER_TIME = model.UNREGISTER_TIME;
				entity.UNREGISTER_REASON = model.UNREGISTER_REASON;
  

                if (m_Rep.Create(entity))
                {
                    return true;
                }
                else
                {
                    errors.Add(Resource.InsertFail);
                    return false;
                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

		
		 public virtual bool Deletes(ref ValidationErrors errors,Expression<Func<PUB_SCHEME, bool>> whereLambda)
         {
		        try
				{
					using (DBContainer db = new DBContainer())
					{
				
							foreach (var entity in db.PUB_SCHEME.Where(whereLambda))
							{
								db.Entry(entity).State = System.Data.Entity.EntityState.Deleted;
							}

							if(db.SaveChanges()>0)
							{
								return true;
							}
							else
							{
								return false;
							}
			
					}
				}
				catch (Exception ex)
				{
					errors.Add(ex.Message);
					ExceptionHander.WriteException(ex);
					return false;
				}
        }

         public virtual bool Delete(ref ValidationErrors errors, object id)
        {
            try
            {
                if (m_Rep.Delete(id) == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

        public virtual bool Delete(ref ValidationErrors errors, object[] deleteCollection)
        {
            try
            {
                if (deleteCollection != null)
                {
                    using (TransactionScope transactionScope = new TransactionScope())
                    {
                        if (m_Rep.Delete(deleteCollection) == deleteCollection.Length)
                        {
                            transactionScope.Complete();
                            return true;
                        }
                        else
                        {
                            Transaction.Current.Rollback();
                            return false;
                        }
                    }
                }
                return false;
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

		
       

        public virtual bool Edit(ref ValidationErrors errors, PUB_SCHEMEModel model)
        {
            try
            {
                PUB_SCHEME entity = m_Rep.GetById(model.CAR_ID);
                if (entity == null)
                {
                    errors.Add(Resource.Disable);
                    return false;
                }
                              				entity.CAR_ID = model.CAR_ID;
				entity.RFID = model.RFID;
				entity.LPR = model.LPR;
				//entity.DRIVER_IDCARD = model.DRIVER_IDCARD;
				//entity.DRIVER_PHONE = model.DRIVER_PHONE;
                entity.DRIVER_IDCARD = model.DRIVER_IDCARD=="0" ? "0" : model.DRIVER_IDCARD;
                entity.DRIVER_PHONE = model.DRIVER_PHONE =="0"? "0": model.DRIVER_PHONE;
                entity.SPECIFIED_WEIGHT = model.SPECIFIED_WEIGHT;
				entity.TARE = model.TARE;
				entity.TOTLE_LENGTH = model.TOTLE_LENGTH;
				entity.TOTLE_WIDTH = model.TOTLE_WIDTH;
				entity.LENGTH = model.LENGTH;
				entity.WIDTH = model.WIDTH;
				entity.HEIGHT = model.HEIGHT;
				entity.POSTIONS = model.POSTIONS;
				entity.REGISTER_USER_ID = model.REGISTER_USER_ID;
				entity.REGISTER_TIME = model.REGISTER_TIME;
				entity.UPDATE_USER_ID = model.UPDATE_USER_ID;
				entity.UPDATE_TIME = model.UPDATE_TIME;
				entity.STATUS = model.STATUS;
				entity.UNREGISTER_TIME = model.UNREGISTER_TIME;
				entity.UNREGISTER_REASON = model.UNREGISTER_REASON;
 


                if (m_Rep.Edit(entity))
                {
                    return true;
                }
                else
                {
                    errors.Add(Resource.NoDataChange);
                    return false;
                }

            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

      

        public virtual PUB_SCHEMEModel GetById(object id)
        {
            if (IsExists(id))
            {
                PUB_SCHEME entity = m_Rep.GetById(id);
                PUB_SCHEMEModel model = new PUB_SCHEMEModel();
                              				model.CAR_ID = entity.CAR_ID;
				model.RFID = entity.RFID;
				model.LPR = entity.LPR;
				model.DRIVER_IDCARD = entity.DRIVER_IDCARD;
				model.DRIVER_PHONE = entity.DRIVER_PHONE;
				model.SPECIFIED_WEIGHT = entity.SPECIFIED_WEIGHT;
				model.TARE = entity.TARE;
				model.TOTLE_LENGTH = entity.TOTLE_LENGTH;
				model.TOTLE_WIDTH = entity.TOTLE_WIDTH;
				model.LENGTH = entity.LENGTH;
				model.WIDTH = entity.WIDTH;
				model.HEIGHT = entity.HEIGHT;
				model.POSTIONS = entity.POSTIONS;
				model.REGISTER_USER_ID = entity.REGISTER_USER_ID;
				model.REGISTER_TIME = entity.REGISTER_TIME;
				model.UPDATE_USER_ID = entity.UPDATE_USER_ID;
				model.UPDATE_TIME = entity.UPDATE_TIME;
				model.STATUS = entity.STATUS;
				model.UNREGISTER_TIME = entity.UNREGISTER_TIME;
				model.UNREGISTER_REASON = entity.UNREGISTER_REASON;
 
                return model;
            }
            else
            {
                return null;
            }
        }

		public virtual PUB_SCHEMEModel GetItem(Expression<Func<PUB_SCHEME, bool>> whereLambda)
        {
 
                PUB_SCHEME entity = m_Rep.GetItem(whereLambda);
                if (entity == null) return null;

                PUB_SCHEMEModel model = new PUB_SCHEMEModel();
                              				model.CAR_ID = entity.CAR_ID;
				model.RFID = entity.RFID;
				model.LPR = entity.LPR;
				model.DRIVER_IDCARD = entity.DRIVER_IDCARD;
				model.DRIVER_PHONE = entity.DRIVER_PHONE;
				model.SPECIFIED_WEIGHT = entity.SPECIFIED_WEIGHT;
				model.TARE = entity.TARE;
				model.TOTLE_LENGTH = entity.TOTLE_LENGTH;
				model.TOTLE_WIDTH = entity.TOTLE_WIDTH;
				model.LENGTH = entity.LENGTH;
				model.WIDTH = entity.WIDTH;
				model.HEIGHT = entity.HEIGHT;
				model.POSTIONS = entity.POSTIONS;
				model.REGISTER_USER_ID = entity.REGISTER_USER_ID;
				model.REGISTER_TIME = entity.REGISTER_TIME;
				model.UPDATE_USER_ID = entity.UPDATE_USER_ID;
				model.UPDATE_TIME = entity.UPDATE_TIME;
				model.STATUS = entity.STATUS;
				model.UNREGISTER_TIME = entity.UNREGISTER_TIME;
				model.UNREGISTER_REASON = entity.UNREGISTER_REASON;
 
                return model; 
        }


		 /// <summary>
        /// 校验Excel数据,这个方法一般用于重写校验逻辑
        /// </summary>
        public virtual bool CheckImportData(string fileName, List<PUB_SCHEMEModel> list,ref ValidationErrors errors )
        {
          
            var targetFile = new FileInfo(fileName);

            if (!targetFile.Exists)
            {

                errors.Add("导入的数据文件不存在");
                return false;
            }

            var excelFile = new ExcelQueryFactory(fileName);

            //对应列头
			 				 excelFile.AddMapping<PUB_SCHEMEModel>(x => x.CAR_ID, "CAR_ID");
				 excelFile.AddMapping<PUB_SCHEMEModel>(x => x.RFID, "RFID");
				 excelFile.AddMapping<PUB_SCHEMEModel>(x => x.LPR, "LPR");
				 excelFile.AddMapping<PUB_SCHEMEModel>(x => x.DRIVER_IDCARD, "DRIVER_IDCARD");
				 excelFile.AddMapping<PUB_SCHEMEModel>(x => x.DRIVER_PHONE, "DRIVER_PHONE");
				 excelFile.AddMapping<PUB_SCHEMEModel>(x => x.SPECIFIED_WEIGHT, "SPECIFIED_WEIGHT");
				 excelFile.AddMapping<PUB_SCHEMEModel>(x => x.TARE, "TARE");
				 excelFile.AddMapping<PUB_SCHEMEModel>(x => x.TOTLE_LENGTH, "TOTLE_LENGTH");
				 excelFile.AddMapping<PUB_SCHEMEModel>(x => x.TOTLE_WIDTH, "TOTLE_WIDTH");
				 excelFile.AddMapping<PUB_SCHEMEModel>(x => x.LENGTH, "LENGTH");
				 excelFile.AddMapping<PUB_SCHEMEModel>(x => x.WIDTH, "WIDTH");
				 excelFile.AddMapping<PUB_SCHEMEModel>(x => x.HEIGHT, "HEIGHT");
				 excelFile.AddMapping<PUB_SCHEMEModel>(x => x.POSTIONS, "POSTIONS");
				 excelFile.AddMapping<PUB_SCHEMEModel>(x => x.REGISTER_USER_ID, "REGISTER_USER_ID");
				 excelFile.AddMapping<PUB_SCHEMEModel>(x => x.REGISTER_TIME, "REGISTER_TIME");
				 excelFile.AddMapping<PUB_SCHEMEModel>(x => x.UPDATE_USER_ID, "UPDATE_USER_ID");
				 excelFile.AddMapping<PUB_SCHEMEModel>(x => x.UPDATE_TIME, "UPDATE_TIME");
				 excelFile.AddMapping<PUB_SCHEMEModel>(x => x.STATUS, "STATUS");
				 excelFile.AddMapping<PUB_SCHEMEModel>(x => x.UNREGISTER_TIME, "UNREGISTER_TIME");
				 excelFile.AddMapping<PUB_SCHEMEModel>(x => x.UNREGISTER_REASON, "UNREGISTER_REASON");
 
            //SheetName
            var excelContent = excelFile.Worksheet<PUB_SCHEMEModel>(0);
            int rowIndex = 1;
            //检查数据正确性
            foreach (var row in excelContent)
            {
                var errorMessage = new StringBuilder();
                var entity = new PUB_SCHEMEModel();
						 				  entity.CAR_ID = row.CAR_ID;
				  entity.RFID = row.RFID;
				  entity.LPR = row.LPR;
				  entity.DRIVER_IDCARD = row.DRIVER_IDCARD;
				  entity.DRIVER_PHONE = row.DRIVER_PHONE;
				  entity.SPECIFIED_WEIGHT = row.SPECIFIED_WEIGHT;
				  entity.TARE = row.TARE;
				  entity.TOTLE_LENGTH = row.TOTLE_LENGTH;
				  entity.TOTLE_WIDTH = row.TOTLE_WIDTH;
				  entity.LENGTH = row.LENGTH;
				  entity.WIDTH = row.WIDTH;
				  entity.HEIGHT = row.HEIGHT;
				  entity.POSTIONS = row.POSTIONS;
				  entity.REGISTER_USER_ID = row.REGISTER_USER_ID;
				  entity.REGISTER_TIME = row.REGISTER_TIME;
				  entity.UPDATE_USER_ID = row.UPDATE_USER_ID;
				  entity.UPDATE_TIME = row.UPDATE_TIME;
				  entity.STATUS = row.STATUS;
				  entity.UNREGISTER_TIME = row.UNREGISTER_TIME;
				  entity.UNREGISTER_REASON = row.UNREGISTER_REASON;
 
                //=============================================================================
                if (errorMessage.Length > 0)
                {
                    errors.Add(string.Format(
                        "第 {0} 列发现错误：{1}{2}",
                        rowIndex,
                        errorMessage,
                        "<br/>"));
                }
                list.Add(entity);
                rowIndex += 1;
            }
            if (errors.Count > 0)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        public virtual void SaveImportData(IEnumerable<PUB_SCHEMEModel> list)
        {
            try
            {
                using (DBContainer db = new DBContainer())
                {
                    foreach (var model in list)
                    {
                        PUB_SCHEME entity = new PUB_SCHEME();
                       						entity.CAR_ID = model.CAR_ID;
						entity.RFID = model.RFID;
						entity.LPR = model.LPR;
						entity.DRIVER_IDCARD = model.DRIVER_IDCARD;
						entity.DRIVER_PHONE = model.DRIVER_PHONE;
						entity.SPECIFIED_WEIGHT = model.SPECIFIED_WEIGHT;
						entity.TARE = model.TARE;
						entity.TOTLE_LENGTH = model.TOTLE_LENGTH;
						entity.TOTLE_WIDTH = model.TOTLE_WIDTH;
						entity.LENGTH = model.LENGTH;
						entity.WIDTH = model.WIDTH;
						entity.HEIGHT = model.HEIGHT;
						entity.POSTIONS = model.POSTIONS;
						entity.REGISTER_USER_ID = model.REGISTER_USER_ID;
						entity.REGISTER_TIME = model.REGISTER_TIME;
						entity.UPDATE_USER_ID = model.UPDATE_USER_ID;
						entity.UPDATE_TIME = model.UPDATE_TIME;
						entity.STATUS = model.STATUS;
						entity.UNREGISTER_TIME = model.UNREGISTER_TIME;
						entity.UNREGISTER_REASON = model.UNREGISTER_REASON;
 
                        db.PUB_SCHEME.Add(entity);
                    }
                    db.SaveChanges();
                }
            }
            catch
            {
                throw;
            }
        }


        public virtual bool IsExists(object id)
        {
            return m_Rep.IsExist(id);
        }
		  public void Dispose()
        { 
            
        }

	}
}
